#include "stm32f10x.h"                  // Device header
#include "delay.h"
#include "gpio.h"
#include "lcd.h"
#include "MYBEEF.h"
#include "WIFI.h"
#include "usart.h"
#include "timer.h"
#include "MYKEY.h"
#include "RS485.h"
#include "rc522.h"
#include "Control.h"
#include "key.h"
#include "MYLED.h"
#include "EC20.h"
#include "MYUSART.h"
#include "stmflash.h"
extern int led;
int flag_use=0;
extern char SN_str[8];
char ka[8]={"F4E9F66A"};
extern int flag;
extern uint32_t  Count_cold;
extern uint32_t Count_hot;
int flag_shui=0;

//const char  MQTT_IP="iot-06z00jk7528mq9m.mqtt.iothub.aliyuncs.com";
//const char MQTT_IP[]={"iot-06z00jk7528mq9m.mqtt.iothub.aliyuncs.com"};

//const char  Device="xujing2";
//const char Device[]={"xujing2"};
//const char  Theme="/sys/k1xilipKXLo/xujing2/thing/event/property/post";
//char  buf={params:{Water:800}};
//const uint16_t port=1883;
//const  char Theme[]={"/sys/k1xilipKXLo/xujing2/thing/event/property/post"};
//const  char buf[]={"{params:{Water:800}}"};


void WiFI_fasong(int a)
{
		char str[100];
		sprintf(str,"Count_hot:%d",a);
  WiFi.SendData (str);

}
int main(void)
{
	delay_init();	    	 //延时函数初始化
	
	TIM3_Int_Init(99,7199);//10ms

	LCD.Init();

	LCD.Clear (BLACK );
    LCD.ShowString(90,80,"Repairing");//显示英文
    MYLED_Init();
	
	MYBuzzer_Init();
	Motor_Init();
	Motor_SetSpeed(0);
	Uart1.Init(115200);
	Uart2.Init(115200);
	Uart5.Init(115200);
    EC20.Init();
	Uart2.SendString("test!!");
	Key.Init ();

	WiFi.Init("915","1223334444"); 
	WiFi.TcpClient("192.168.31.110",8080);
	WiFi.SendData("RequestToUse");	


	NVIC_Configuration(); 		//设置NVIC中断分组2:2位抢占优先级，2位响应优先级

/***************显示屏测试**********************************************
	LCD.ShowNum(0,0,100,3);
	LCD.ShowChinese16x16(120,20,"实验"); //显示 中文字符串
	LCD.ShowString(160,20,"5");
	LCD.ShowString(80,80,"This is a test!");//显示英文字符串
	LCD.ShowChinese16x16(80,120,"这是一条测试语句！");
***********************************************************************/		

//  LCD.ShowNum(0,0,100,3);
//	LCD_Show_One_Chinese16x16(120,20,"实验");
//	LCD.ShowChinese16x16(120,20,""); //显示 中文字符串
//	LCD.ShowString(160,20,"5");
	EC20.MQTT_OpenNetwork ("iot-06z00c0bmcciwel.mqtt.iothub.aliyuncs.com",1883);
	EC20.Connect_MQTT("clientExampie");
	AKST (2000);

	LCD.ShowString(90,80,"Repairing");//显示英文字符串

    Uart2.SendChar('1');
	Uart2.SendString("123456");


/**********RC522读卡测试**************	
	RC522_Init(); //读卡器初始化
	RC522_Read(0x01);
	LCD.ShowString(0,0,SN_str);
**************************************/	
/**********WIFI测试**************		
	Uart1.Init(115200);
	WiFi.Init("915","1223334444");
	WiFi.TcpClient("192.168.31.113",6060);
	WiFi.SendData("RequestToUse");	
******************************/		

	RC522_Init(); //读卡器初始化
	
	Uart1.Init(115200);

	while(1)
	{		
		RC522_Read(0x01);
		//uint16_t  hot_qian=34;
		//uint16_t cold_qian=34;
		//const uint8_t fasong[2]={hot_qian,cold_qian};
		
		if(led==1){  
//			Uart1.SendCmd ( fasong ,4);
			if(Count_hot <=1500){
				if(flag_shui ==0){
					WiFi.SendData("RequestTooff");	
					flag_shui =1;
				}
			}

			if(flag_use ==1){
			MYKEY_Scan();
			}

			if(flag ==1){
				if(flag_shui ==0){
					
					LCD.ShowString(0,0,"ID:");//显示英文字符串
					LCD.ShowString(30,0,SN_str);
							
					if(strcmp (ka,SN_str)==0){
					flag_use=1;
					LCD.ShowString(120,0,"Success");//显示英文字符串
					MYBuzzer_On();
					delay_ms(500);
					MYBuzzer_Off();
					//	MYKEY_Scan();
					}
					else {
						LCD.ShowString(120,0,"Erro");//显示英文字符串
						delay_ms(500);
						flag_use=0; 
					}
									
					flag =0;
				}   			
				else if(flag_shui ==1){
					flag_use=1;
					LCD.ShowString(0,0,"ID:");//显示英文字符串
					LCD.ShowString(30,0,SN_str);
					LCD.ShowString(120,0,"Success");//显示英文字符串
					MYBuzzer_On();
					delay_ms(500);
					MYBuzzer_Off();
					flag=0;
				}
		    }
			LCD.ShowString(0,0,"                            ");
/*****************************************/			
//		if(Uart1.RecvLen!=0)
//		{
//			Uart2.SendString(Uart1.RecvBuf);
//			Uart1.ClrRxBuf();
//		}		
//		if(Uart2.RecvLen!=0)
//		{
//			Uart1.SendString(Uart2.RecvBuf);
//			Uart2.ClrRxBuf();
//		}
/*****************************************/		
    		LCD.ShowString(90,80,"   Using    ");//显示英文字符串
		}
		else if(led==2){
			LCD.ShowString(90,80,"   Pausing    ");//显示英文字符串 
		}
	}
	if(Uart1.RecvLen!=0){
			Uart2.SendString(Uart1.RecvBuf);
			Uart1.ClrRxBuf();
	}		
	if(Uart2.RecvLen!=0){
			Uart1.SendString(Uart2.RecvBuf);
			Uart2.ClrRxBuf();
	}

}

